package org.tio.sitexxx.im.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.HandshakeResp;
import org.tio.sitexxx.service.cache.CacheConfig;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.vo.GroupStat;
import org.tio.sitexxx.service.vo.LoadData;
import org.tio.utils.cache.ICache;

/**
 * @author tanyaowu 
 * 2016年9月8日 下午1:35:02
 */
public class Ims {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(Ims.class);

	/**
	 * 
	 * @author: tanyaowu
	 */
	public Ims() {
	}

	/**
	 * 
	 * @param channelContext
	 * @param imPacket
	 */
	public static void send(ChannelContext channelContext, ImPacket imPacket) {
		Tio.send(channelContext, imPacket);
	}

	/**
	 * 发送握手包
	 * @param channelContext
	 * @author tanyaowu
	 */
	public static void sendHandshake(ChannelContext channelContext) {
		HandshakeResp handshakeResp = new HandshakeResp();
		handshakeResp.setCid(channelContext.getId());
		ImPacket handshakeRespPacket = new ImPacket(Command.HANDSHAKE_RESP, handshakeResp);
		Ims.send(channelContext, handshakeRespPacket);
	}

	/**
	 * 
	 * @param groupid
	 * @param imPacket
	 */
	public static void sendToGroup(String groupid, ImPacket imPacket) {
		Tio.sendToGroup(TioSiteImServerStarter.serverGroupContextWs, groupid, imPacket);
	}

	/**
	 * 
	 * @param userid
	 * @param imPacket
	 */
	public static void sendToUser(String userid, ImPacket imPacket) {
		Tio.sendToUser(TioSiteImServerStarter.serverGroupContextWs, userid, imPacket);
	}

	/**
	 * 
	 * @param userid
	 * @param imPacket
	 */
	public static void sendToUser(Integer userid, ImPacket imPacket) {
		sendToUser("" + userid, imPacket);
	}

	/**
	 * 
	 * @param group
	 * @param channelContext
	 * @return
	 */
	public static boolean isInGroup(String group, ChannelContext channelContext) {
		return Tio.isInGroup(group, channelContext);
	}

	/**
	 * 
	 * @param token
	 * @param imPacket
	 */
	public static void sendToToken(String token, ImPacket imPacket) {
		Tio.sendToToken(TioSiteImServerStarter.serverGroupContextWs, token, imPacket);
	}

	/**
	 * 获取当前有多少ip数，包含机器
	 * @return
	 */
	public static int getIpCount() {
		return TioSiteImServerStarter.serverGroupContextApp.ips.getIpmap().size();
	}
	
	/**
	 * @param ip
	 * @param imPacket
	 */
	public static void sendToIp(String ip, ImPacket imPacket) {
		Tio.sendToIp(TioSiteImServerStarter.serverGroupContextWs, ip, imPacket);
	}

	/**
	 * 发送到指定的channelContextId
	 * @param channelContextId
	 * @param imPacket
	 */
	public static void sendToId(String channelContextId, ImPacket imPacket) {
		Tio.sendToId(TioSiteImServerStarter.serverGroupContextWs, channelContextId, imPacket);
	}

	public static ChannelContext getByCid(String cid) {
		return Tio.getChannelContextById(TioSiteImServerStarter.serverGroupContextWs, cid);
	}

	/**
	 * 某群组有多少用户
	 * @param group
	 * @return
	 */
	public static int groupCount(String group) {
		int c1 = Tio.groupCount(TioSiteImServerStarter.serverGroupContextWs, group);
		return c1;
	}

	/**
	 * 
	 * @param imPacket
	 * @author: tanyaowu
	 */
	public static void sendToAll(ImPacket imPacket) {
		Tio.sendToAll(TioSiteImServerStarter.serverGroupContextWs, imPacket);
	}

	/**
	 * 
	 * @param group
	 * @param imPacket
	 */
	public static void notifyClusterForGroup(Integer group, ImPacket imPacket) {
		notifyClusterForGroup(group + "", imPacket);
	}

	/**
	 * 
	 * @param group
	 * @param imPacket
	 */
	public static void notifyClusterForGroup(String group, ImPacket imPacket) {
		boolean isGroupCluster = false;//P.getBoolean("im.group.cluster", false);
		if (!isGroupCluster) {
			Tio.notifyClusterForGroup(TioSiteImServerStarter.serverGroupContextWs, group, imPacket);
		}
	}

	/**
	 * 
	 * @return
	 * @author: tanyaowu
	 */
	public static LoadData createLoadData() {
		LoadData loadData = new LoadData();
		loadData.setPcCount(TioSiteImServerStarter.serverGroupContextWs.connections.size());
		return loadData;
	}

	/**
	 * 
	 * @param group
	 * @return
	 */
	public static GroupStat createGroupStat(String group) {
		int maxRealOnlineInCache = 0; //在缓存中的在线峰值（真实的）

		ICache groupStatCache = Caches.getCache(CacheConfig.GROUP_STAT);
		GroupStat groupStat = groupStatCache.get(group, GroupStat.class);
		if (groupStat != null) {
			maxRealOnlineInCache = groupStat.getMaxOnline();
		}
		groupStat = Ims.createGroupStat1(group);

		groupStat.setMaxOnline(Math.max(maxRealOnlineInCache, groupStat.getMaxOnline()));

		groupStatCache.put(group, groupStat);

		return groupStat;
	}

	/**
	 * 
	 * @param group
	 * @return
	 */
	private static GroupStat createGroupStat1(String group) {
		GroupStat groupStat = new GroupStat();

		int online = Ims.groupCount(group);

		int maxRealOnline = online;

		groupStat.setOnline(online);

		groupStat.setMaxOnline(maxRealOnline);

		return groupStat;
	}

}
